package com.flyco.modules.system.controller;

import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.system.vo.LoginUser;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.service.IStmStoreInfoService;
import com.flyco.modules.system.entity.SysMsgNotice;
import com.flyco.modules.system.entity.SysMsgNoticeRecord;
import com.flyco.modules.system.service.ISysMsgNoticeRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 消息通知记录
 * @Author: flyco
 * @Date:   2024-06-19
 * @Version: V1.0
 */
@Slf4j
@Api(tags="消息通知记录")
@RestController
@RequestMapping("/f/system/sysMsgNoticeRecord")
public class SysMsgNoticeRecordController extends BaseController<SysMsgNoticeRecord, ISysMsgNoticeRecordService> {
	@Autowired
	private ISysMsgNoticeRecordService sysMsgNoticeRecordService;
	 @Autowired
	 private IStmStoreInfoService stmStoreInfoService;
	/**
	 * 分页列表查询
	 *
	 * @param sysMsgNoticeRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "消息通知记录-分页列表查询")
	@ApiOperation(value="消息通知记录-分页列表查询", notes="消息通知记录-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SysMsgNoticeRecord sysMsgNoticeRecord,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysMsgNoticeRecord> queryWrapper = QueryGenerator.initQueryWrapper(sysMsgNoticeRecord, req.getParameterMap());
		Page<SysMsgNoticeRecord> page = new Page<SysMsgNoticeRecord>(pageNo, pageSize);
		IPage<SysMsgNoticeRecord> pageList = sysMsgNoticeRecordService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 * 添加
	 *
	 * @param sysMsgNoticeRecord
	 * @return
	 */
	@AutoLog(value = "消息通知记录-添加")
	@ApiOperation(value="消息通知记录-添加", notes="消息通知记录-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SysMsgNoticeRecord sysMsgNoticeRecord) {
		sysMsgNoticeRecordService.save(sysMsgNoticeRecord);
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param sysMsgNoticeRecord
	 * @return
	 */
	@AutoLog(value = "消息通知记录-编辑")
	@ApiOperation(value="消息通知记录-编辑", notes="消息通知记录-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SysMsgNoticeRecord sysMsgNoticeRecord) {
		sysMsgNoticeRecordService.updateById(sysMsgNoticeRecord);
		return Result.ok("编辑成功!");
	}

	 /**
	  * 通过id确认消息
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "消息通知记录-通过id确认消息")
	 @ApiOperation(value="消息通知记录-通过id确认消息", notes="消息通知记录-通过id确认消息")
	 @PostMapping(value = "/confirm")
	 public Result<?> confirm(@RequestParam(name="id",required=true) String id) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 SysMsgNoticeRecord sysMsgNoticeRecord = sysMsgNoticeRecordService.getById(id);
		 if (sysMsgNoticeRecord != null) {
			 sysMsgNoticeRecord.setViewedStatus(1);
			 sysMsgNoticeRecord.setViewedBy(sysUser.getId());
			 sysMsgNoticeRecord.setViewedTime(new Date());
			 sysMsgNoticeRecordService.updateById(sysMsgNoticeRecord);
		 }
		 return Result.ok("确认成功!");
	 }

	 /**
	  * 分页列表查询
	  *
	  * @return
	  */
	 @AutoLog(value = "消息通知记录-分页列表查询")
	 @ApiOperation(value="消息通知记录-分页列表查询", notes="消息通知记录-分页列表查询")
	 @GetMapping(value = "/notReadList")
	 public Result<?> notReadList() {
		 List<Long> idList = new ArrayList<>();
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 QueryWrapper<StmStoreInfo> storeQuery = new QueryWrapper<>();
		 storeQuery.eq("account", sysUser.getUsername());
		 storeQuery.eq("partner_id", sysUser.getPartnerId());
		 List<StmStoreInfo> storeInfos = stmStoreInfoService.list(storeQuery);
		 if (storeInfos != null && !storeInfos.isEmpty()) {
			 StmStoreInfo storeInfo = storeInfos.get(0);
			 QueryWrapper<SysMsgNoticeRecord> queryWrapper = new QueryWrapper<>();
			 queryWrapper.eq("viewed_status", 0);
			 queryWrapper.eq("store_id", storeInfo.getStoreId());
			 List<SysMsgNoticeRecord> resultList = sysMsgNoticeRecordService.list(queryWrapper);
			 if (!resultList.isEmpty()) {
				 resultList.forEach(x -> {
					 idList.add(x.getNoticeId());
				 });
			 }
		 }
		 return Result.ok(idList);
	 }

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "消息通知记录-通过id删除")
	@ApiOperation(value="消息通知记录-通过id删除", notes="消息通知记录-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		sysMsgNoticeRecordService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "消息通知记录-批量删除")
	@ApiOperation(value="消息通知记录-批量删除", notes="消息通知记录-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysMsgNoticeRecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "消息通知记录-通过id查询")
	@ApiOperation(value="消息通知记录-通过id查询", notes="消息通知记录-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SysMsgNoticeRecord sysMsgNoticeRecord = sysMsgNoticeRecordService.getById(id);
		return Result.ok(sysMsgNoticeRecord);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param sysMsgNoticeRecord
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, SysMsgNoticeRecord sysMsgNoticeRecord) {
      return super.exportXls(request, sysMsgNoticeRecord, SysMsgNoticeRecord.class, "消息通知记录");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, SysMsgNoticeRecord.class);
  }

}
